<template>
    <el-container>
        <el-header height="auto">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="团队">
                    <el-select v-model="selectteamvalue" placeholder="Select">
                        <el-option v-for="item in Object.keys(selectteam)" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="时间">
                    <el-date-picker v-model="time" type="daterange" :shortcuts="shortcuts"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="start">查询</el-button>
                </el-form-item>
            </el-form>
        </el-header>
        <el-main>
            <el-table border :data="tableData.filter(e => {
                return selectteam[selectteamvalue].length ? selectteam[selectteamvalue].includes(e.id) : true
            })" style="width: 100%" height="65vh">
                <el-table-column prop="id" sortable label="id" width="90" />
                <el-table-column prop="name" label="团队名称" />
                <el-table-column sortable prop="selectusercountgroupteams" label="总人数" width="100" />
                <el-table-column sortable prop="selectusercountgroupteam" label="新增人数" width="100" />
                <el-table-column sortable prop="selectuserlasttimecountgroupteam" label="登录人数" width="100" />
                <el-table-column sortable prop="selectuserquestiondistinctgroupteam" label="练习做题人数" width="120" />
                <el-table-column sortable prop="selectuserquestiongroupteam" label="练习做题次数" width="120" />
                <el-table-column sortable prop="selectuserquestionacgroupteam" label="做题正确数" width="120" />
                <el-table-column sortable prop="matchuser" label="比赛做题人数" width="120" />
                <el-table-column sortable prop="selectvideodistinct" label="观看视频人数" width="120" />
                <el-table-column sortable prop="selectvideo" label="观看视频次数" width="120" />
            </el-table>
        </el-main>
    </el-container>
</template>

<script setup lang="ts">
import FetchApi from '../../../logs/api';
import { compile, onMounted, reactive, ref } from 'vue'
import Studycodemodel from '../../../mysql_interface/studycodemodel';
import { useStore } from 'vuex';
import { Storekey } from '../../../interface';
import { asyncselectstatistics, asyncselectteams, IselectstatisticsType } from '../../../api';
import dayjs, { Dayjs } from 'dayjs';
import { dealy, shortcuts } from '../../../util/time';
type RangeValue = [Dayjs, Dayjs];
const selectteam = {
    全部: [],
    昌平: [1091, 1092, 1093, 1094, 1096, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1137, 1138, 1139, 1140, 1142, 1143, 1144, 1145, 1146, 1149],
    销售可见: []
}

const selectteamvalue = ref('全部')


const date = dayjs()

const time = ref([new Date(Date.now() - 1000 * 60 * 60 * 24 * 365), new Date()])

const formInline = reactive<{
    team?: number[]
}>({
    team: undefined
})

const store = useStore(Storekey)

const teams = ref<Array<{ label?: string, value?: number }>>([])

const teamuser = ref<{ teamid: number, count: number }[]>([])

const teamlasttime = ref<{ teamid: number, count: number }[]>([])

const tableData = ref<Array<{
    id: number | undefined;
    name: string | undefined;
    selectusercountgroupteam: number | undefined;
    selectuserlasttimecountgroupteam: number | undefined;
    selectuserquestionacgroupteam: number | undefined;
    selectuserquestiongroupteam: number | undefined;
}>>([])

// 团队用户参加比赛次数


// 团队用户提交题目数量
const teamuserquestion = ref<{ teamid: number, count: number }[]>([])

// 团队所有用户观看视频次数
const teamvideo = ref<{ teamid: number, count: number }[]>([])

// 团队观看过视频人数
const teamuservideo = ref<{ teamid: number, count: number }[]>([])

async function getteamuservideo(): Promise<{ count: string, userid: string }[]> {

    const { data } = await FetchApi({
        from: dayjs(time.value[0]).toDate(),
        to: dayjs(time.value[1]).toDate(),
        query: 'type:video | SELECT count(_userid_) as count ,_userid_ as userid GROUP by userid limit 0,10000',
        projectName: "htetest1",
        logstoreName: "ojhte",
        reverse: true
    })
    return data
}

async function getteamuservideodistinct(): Promise<{ count: string, userid: string }[]> {
    const { data } = await FetchApi({
        from: dayjs(time.value[0]).toDate(),
        to: dayjs(time.value[1]).toDate(),
        query: 'type:video | SELECT DISTINCT(_userid_) as userid limit 0,10000 ',
        projectName: "htetest1",
        logstoreName: "ojhte",
        reverse: true
    })
    return data
}

async function getteam() {
    const { rows } = await asyncselectteams({
        _limit: 10000
    })
    selectteam.销售可见 = rows.filter(e => !e.isadmin).map(e => Number(e.ID))
    teams.value = rows.map(e => {
        return {
            value: e.ID,
            label: e.name
        }
    })
}

async function selectquestion(type: IselectstatisticsType[] = ['selectusercountgroupteam', 'selectuserlasttimecountgroupteam', 'selectuserquestionacgroupteam', 'selectuserquestiongroupteam', 'selectuserquestiondistinctgroupteam', 'selectusercountgroupteams']) {
    return asyncselectstatistics({
        from: dayjs(time.value[0]).unix() * 1000,
        to: dayjs(time.value[1]).unix() * 1000,
        type
    })
}


onMounted(async () => {
    await getteam()
    start()

})

async function start() {
    const data = await selectquestion()
    const _teams = formInline.team?.length ? formInline.team.map(e => {
        const f = teams.value.find(c => c.value == e)
        return {
            value: f?.value,
            label: f?.label
        }
    }) : teams.value

    const _table = _teams.map(e => {
        const selectusercountgroupteam = data.selectusercountgroupteam.find(c => c.teamid == e.value)
        const selectusercountgroupteams = data.selectusercountgroupteams.find(c => c.teamid == e.value)
        const selectuserlasttimecountgroupteam = data.selectuserlasttimecountgroupteam.find(c => c.teamid == e.value)
        const selectuserquestionacgroupteam = data.selectuserquestionacgroupteam.find(c => c.teamid == e.value)
        const selectuserquestiongroupteam = data.selectuserquestiongroupteam.find(c => c.teamid == e.value)
        const selectuserquestiondistinctgroupteam = data.selectuserquestiondistinctgroupteam.find(c => c.teamid == e.value)
        const matchuser = data.matchuser.find(c => c.teamid == e.value)


        return {
            id: e.value,
            name: e.label,
            selectusercountgroupteam: selectusercountgroupteam?.count || 0,
            selectusercountgroupteams: selectusercountgroupteams?.count || 0,
            selectuserlasttimecountgroupteam: selectuserlasttimecountgroupteam?.count || 0,
            selectuserquestionacgroupteam: selectuserquestionacgroupteam?.count || 0,
            selectuserquestiongroupteam: selectuserquestiongroupteam?.count || 0,
            selectuserquestiondistinctgroupteam: selectuserquestiondistinctgroupteam?.count || 0,
            matchuser: matchuser?.count || 0,
        }
    })
    const teamuser = (data as any).teamdata as Studycodemodel.Iteamuser[]

    tableData.value = _table
    const video = await getteamuservideo()
    const t: [number, number][] = []
    video.forEach(e => {
        const { count, userid } = e
        const user = teamuser.find(e => e.userid === Number(userid))
        if (user) {
            const { teamid } = user
            const z = t.findIndex(v => v[0] === teamid)
            if (z === -1) {
                t.push([teamid, Number(count)])
            } else {
                t[z][1] = t[z][1] + Number(count)
            }
        }
    })

    await dealy(3000)

    const videodistinct = await getteamuservideodistinct()
    const _videodistinct: [number, number][] = []

    videodistinct.forEach(e => {
        const { count = 1, userid } = e
        const user = teamuser.find(e => e.userid === Number(userid))
        if (user) {
            const { teamid } = user
            const z = _videodistinct.findIndex(v => v[0] === teamid)
            if (z === -1) {
                _videodistinct.push([teamid, Number(count)])
            } else {
                _videodistinct[z][1] = _videodistinct[z][1] + Number(count)
            }
        }
    })
    tableData.value = tableData.value.map(e => {
        const selectvideo = t.find(z => z[0] === e.id) || [0, 0]
        const selectvideodistinct = _videodistinct.find(z => z[0] === e.id) || [0, 0]
        return {
            selectvideo: selectvideo[1],
            selectvideodistinct: selectvideodistinct[1],
            ...e
        }
    })
}

</script>